﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using BEGDC.Utility.Castle;
using BEGDC.CC.BL.Common;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Workflow;

namespace BEGDC.CC.Web
{
    /// <summary>
    /// Summary description for ShowTypeForm.
    /// </summary>
    public class ShowCATConfigurationForm : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label lbName;
        protected System.Web.UI.WebControls.TextBox txtName;
        protected System.Web.UI.WebControls.TextBox txtDescription;
        protected System.Web.UI.WebControls.Button btnSave;
        protected System.Web.UI.WebControls.Label lbDescription;
        protected System.Web.UI.WebControls.DropDownList ddlCATPSO;
        protected System.Web.UI.WebControls.Button btnClose;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            if (!Page.IsPostBack)
            {
                InitializeGlobalization();
                string id = Request.QueryString["id"];
                BindUserRole(Convert.ToInt32(id));

                if (((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).Roles.IndexOf("BusinessAdmin") > -1 && ((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).Roles.IndexOf("RoleAdmin") == -1)
                {
                    this.btnSave.Visible = false;
                }

            }
        }

        private void BindUserRole(int id)
        {
            BEGDC.CC.BL.UserManagement.IUser iuser = AppContainer.Instance[ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;
            BEGDC.CC.Entity.UserRole userrole = iuser.GetUserRoleByID(id);

            IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;

            //			System.Collections.IList ilist;
            //			ilist = utility.BuildDicList(BEGDC.CC.BL.Common.DicType.CAT);

            System.Data.DataTable dtDept = iuser.GetDepartmentByType(BEGDC.CC.BL.Common.SysDicItem.DepartmentType.CAT);

            //			if(userrole != null)
            //			{
            //				switch(userrole.Role.Id)
            //				{
            //					case 12:
            //						ilist = iuser.GetDepartmentByType(BEGDC.CC.BL.Common.SysDicItem.DepartmentType.);
            //						this.lblTitle.Text = "CAT";
            //						this.lbName.Text = "CAT";
            //						break;
            ////					case 13:
            ////						ilist = utility.BuildDicList(BEGDC.CC.BL.Common.DicType.BRANCH);
            ////						this.lblTitle.Text = "BRANCH";
            ////						this.lbName.Text = "BRANCH";
            ////						break;
            ////
            ////					case 14:
            ////						ilist = utility.BuildDicList(BEGDC.CC.BL.Common.DicType.CAT);
            ////						this.lblTitle.Text = "CAT";
            ////						this.lbName.Text = "CAT";
            ////						break;
            //				}
            ////				this.txtName.Text = dic.UserRoleName;
            ////				this.txtDescription.Text = dic.Description;


            this.ddlCATPSO.Items.Add(new System.Web.UI.WebControls.ListItem(string.Empty, "0"));

            foreach (System.Data.DataRow drDept in dtDept.Rows)
            {
                this.ddlCATPSO.Items.Add(new System.Web.UI.WebControls.ListItem(drDept["departmentcode"].ToString(), drDept["departmentid"].ToString()));
            }
            //				this.ddlCATPSO.DataValueField = "departmentid";
            //				this.ddlCATPSO.DataTextField = "departmentcode";
            //				this.ddlCATPSO.DataSource = dtDept;
            //				this.ddlCATPSO.DataBind();

            if (userrole.CATBranch != null)
                this.ddlCATPSO.SelectedValue = userrole.CATBranch.Id.ToString();
            //			}
        }

        private void InitializeGlobalization()
        {
            //this.lblTitle.Text = "CAT Director";
            this.lbName.Text = "CAT Director (CAT主管): ";
        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        void btnSave_Click(object sender, EventArgs e)
        {
            BEGDC.CC.BL.UserManagement.IUser iuser = AppContainer.Instance[ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;

            int catID = Convert.ToInt32(this.ddlCATPSO.SelectedValue);

            if (iuser.SaveUserRole_CAT(Convert.ToInt32(Request.QueryString["id"]), catID))
            {
                IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;

                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');returnValue='Type';window.close();</script>");
                CCIdentity id = this.User.Identity as CCIdentity;
                BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
                log.Module = "CAT Configuration";
                Employee emp = new Employee();
                emp.Id = id.UserInfo.Id;
                log.Employee = emp;
                log.Time = System.DateTime.Now;
                log.Description = "Update CAT property of Employee .";
                utility.SaveOperationLog(log);
            }
            else
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");

        }

        void btnClose_Click(object sender, EventArgs e)
        {
            Page.RegisterClientScriptBlock("", "<script>returnValue='';window.close();</script>");
        }
    }
}
